from concurrent import futures
import logging
import time
import grpc
import test_pb2
import test_pb2_grpc


class StreamTest(test_pb2_grpc.StreamTestServicer):
    def ClientStream(self, request_iterator, context):
        for i in request_iterator:
            print(i.message)

        return test_pb2.response(message='ok')


# 若想中断传输，客户端可以在接受函数中break或者return
def serve():
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
    test_pb2_grpc.add_StreamTestServicer_to_server(StreamTest(), server)
    server.add_insecure_port('[::]:10086')
    server.start()
    server.wait_for_termination()


if __name__ == '__main__':
    logging.basicConfig()
    serve()
